window.onload = function (e) {
    e = e || window.event;
    e.returnValue = false;


    //获取元素
    let register_box = document.querySelector(".register_box");
    let center_right = document.querySelector(".center_right");
    let even = document.querySelector(".even");
    let car=document.querySelector(".car");
    let surname = document.querySelector(".surname");
    let name = document.querySelector(".name");
    let years = document.querySelector(".years");
    let month = document.querySelector(".month");
    let day = document.querySelector(".day");
    let phoneNumber = document.querySelector(".phoneNumber");
    let yanzhengma = document.querySelector(".yanzhengma");
    let yanzheng = document.querySelector(".yanzheng");
    let phoneyanzhengma = document.querySelector(".phoneyanzhengma");
    let phoneyanzheng = document.querySelector(".phoneyanzheng");
    let Email = document.querySelector(".Email");
    let passWord = document.querySelector(".passWord");
    let add_self = document.querySelector(".add_self");

    //错误信息
    let surnameError = document.querySelector(".surnameError");
    let nameError = document.querySelector(".nameError");
    let birthdayError = document.querySelector(".birthdayError");
    let phoneError = document.querySelector(".phoneError");
    let yanzhengError = document.querySelector(".yanzhengError");
    let p_Error = document.querySelector(".p_Error");
    let E_error = document.querySelector(".E_error");
    let passError = document.querySelector(".passError");

    let p_yanzheng;

    //判断带星号的信息是否填写完成
    let flag = {
        surnameFlag: false,
        nameFlag: false,
        phoneFlag: false,
        phoneyanzhengFlag: false,
        passFlag: false
    }

    //用来存放用户信息
    let user = {
        name: "",
        mobilephone: "",
        youxiang: "",
    }



    //事件委托（点击事件）
    register_box.onclick = function (e) {
        if (e.target.classList.contains("find")) {
            center_right.style.right = 0;
            even.style.display = 'block';
        }

        if (e.target.classList.contains("cancle")) {
            center_right.style.right = "-511px";
            even.style.display = 'none';
        }

        //获取验证码
        if (e.target.classList.contains("yanzheng")) {
            let number = Math.floor((Math.random() * 9 + 1) * 100000);
            yanzheng.innerHTML = number;
        }

        //获取手机验证码
        if (e.target.classList.contains("phoneyanzheng")) {
            let p_number = Math.floor((Math.random() * 9 + 1) * 1000);
            p_yanzheng = p_number;
            confirm("手机验证码：" + p_number);
            if (!confirm || confirm) {
                return false;
            }
        }

        if (e.target.classList.contains("shoppingcar")) {
            let user = getCookie("login");
            if (!user) {
                localStorage.setItem("url", location.href);
                confirm("请先登录!");
                if (confirm) {
                    location.href = "../html/login.html";
                    return;
                }
            }
            car.style.right = 0;
            even.style.display = 'block';
        }

        if (e.target.classList.contains("quxiao")) {
            car.style.right = "-511px";
            even.style.display = 'none';
        }

    }

    add_self.onclick = function (e) {
        e.preventDefault();
        let r = pAjax({
            url: "/api/register",
            type: "post",
            data: {
                username: surname.value + name.value,
                phone: phoneNumber.value,
                email: Email.value,
                password: passWord.value,
            }
        });
        r.then((res) => {
            res = JSON.parse(res);
            if (!res.code) {
                alert(res.msg);
                phoneNumber.value = " ";
                Email.value = " ";
                return;
            }else{
                alert(res.msg);
                location.href="../html/login.html";
            }
        });
    }



    //注册正则验证
    //姓
    surname.oninput = function () {
        let surreg = /^[a-zA-Z\u4e00-\u9fa5]+$/i;
        let surRes = surreg.test(surname.value);
        if (surname.value.length == 0) {
            surnameError.innerHTML = "请输入姓氏";
            flag.surnameFlag = false;
            fun();
            return;
        }
        if (!surRes) {
            surnameError.innerHTML = "姓氏只能由中文或英文组成";
            flag.surnameFlag = false;
            fun();
            return;
        }
        surnameError.innerHTML = " ";
        flag.surnameFlag = true;
        fun();
    }

    //名
    name.oninput = function () {
        let namereg = /^[a-zA-Z\u4e00-\u9fa5]+$/i;
        let nameRes = namereg.test(name.value);
        if (name.value.length == 0) {
            nameError.innerHTML = "请输入名字";
            flag.nameFlag = false;
            fun();
            return;
        }
        if (!nameRes) {
            nameError.innerHTML = "名字只能由中文或英文组成";
            flag.nameFlag = false;
            fun();
            return;
        }
        nameError.innerHTML = " ";
        flag.nameFlag = true;
        fun();
    }

    //生日
    //年
    years.oninput = function () {
        let yearsreg = /^(192[0-9]|19[3-9]\d|200\d|201[0-9]|202[0-5])$/i;
        let yearsRes = yearsreg.test(years.value);
        if (!yearsRes) {
            birthdayError.innerHTML = "请输入正确的年份";
            return;
        }
        birthdayError.innerHTML = " ";
    }

    //月
    month.oninput = function () {
        let monthreg = /^([1-9]{1}|1[0-2])$/;
        let monthRes = monthreg.test(month.value);
        if (!monthRes) {
            birthdayError.innerHTML = "请输入正确的月份";
            return;
        }
        birthdayError.innerHTML = " ";
    }

    //天
    day.oninput = function () {
        let dayreg = /^([1-9]|1[0-9]|2[0-9]|3[0-1])$/;
        let dayRes = dayreg.test(day.value);
        if (!dayRes) {
            birthdayError.innerHTML = "请输入正确的日期";
            return;
        }
        birthdayError.innerHTML = " ";
    }


    //手机号码
    phoneNumber.oninput = function () {
        let pNumreg = /^1[3-9]\d{9}$/i;
        let pNumReg = pNumreg.test(phoneNumber.value);
        if (!pNumReg) {
            phoneError.innerHTML = "请输入手机号码";
            flag.phoneFlag = false;
            fun();
            return;
        }
        phoneError.innerHTML = " ";
        flag.phoneFlag = true;
        fun();
    }

    //图形验证码
    yanzhengma.oninput = function () {
        if (yanzhengma.value.length == 0) {
            yanzhengError.innerHTML = "请输入图形验证码";
            return;
        }
        if (yanzhengma.value != yanzheng.innerHTML) {
            yanzhengError.innerHTML = "输入错误";
            return;
        }
        yanzhengError.innerHTML = " ";
    }

    //手机验证码
    phoneyanzhengma.oninput = function () {
        if (phoneyanzhengma.value.length == 0) {
            p_Error.innerHTML = "请输入手机验证码";
            flag.phoneyanzhengFlag = false;
            fun();
            return;
        }
        if (phoneyanzhengma.value != p_yanzheng) {
            p_Error.innerHTML = "验证码不正确";
            flag.phoneyanzhengFlag = false;
            fun();
            return;
        }
        p_Error.innerHTML = " ";
        flag.phoneyanzhengFlag = true;
        fun();
    }

    //邮箱
    Email.oninput = function () {
        let emailreg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
        let emailRes = emailreg.test(Email.value);
        if (!emailRes) {
            E_error.innerHTML = "请输入正确的电子邮箱";
        } else {
            E_error.innerHTML = " ";
        }
    }

    //密码
    passWord.oninput = function () {
        let passreg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!+,-./;:==>?@$%&*])[a-zA-Z\d!+,-./;:==>?@$%&*]{8,}$/;
        let passRes = passreg.test(passWord.value);
        if (!passRes || passWord.value.length == 0) {
            passError.style.color = "#c60000";
            flag.passFlag = false;
            fun();
            return;
        } else {
            passError.style.color = "#757575";
            flag.passFlag = true;
            fun();
        }
    }


    //注册接口
    // add_self.onsubmit=function(e){
    //     e = e || window.event;
    //     e.returnValue = false;
    //     e.preventDefault();

    //     let r=pAjax({
    //         url:"/register",
    //         type:"get",
    //         data:{
    //             username:surname.value+name.value,
    //             phone:phoneNumber.value,
    //             email:Email.value,
    //             password:passWord.value,
    //         }
    //     });
    //     r.then((res)=>{
    //         res=JSON.parse(res);
    //         console.log(res);
    //     })

    // }


    //当所有带星号的信息全部填写正确 调用这个函数
    function fun() {
        if (flag.surnameFlag && flag.nameFlag && flag.phoneFlag && flag.phoneyanzhengFlag && flag.passFlag) {
            add_self.removeAttribute("disabled");
            return;
        }
        add_self.setAttribute("disabled", "true");
    }



}